# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QTreeWidgetItem
from qfluentwidgets import TreeWidget as Tree


class TreeWidget(Tree):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.itemChanged.connect(self._handlechanged)

    def _handlechanged(self, item: QTreeWidgetItem, column: int):
        # parent:QTreeWidgetItem = item.parent()
        # if parent:
        #     parent.setCheckState(0, Qt.CheckState.PartiallyChecked)
        count = item.childCount()
        # 如果被选中
        if item.checkState(column) == Qt.CheckState.Checked:
            # 连同下面子子节点全部设置为选中状态
            for f in range(count):
                if item.child(f).checkState(0) != Qt.CheckState.Checked:
                    item.child(f).setCheckState(0, Qt.CheckState.Checked)
        # 如果取消选中
        if item.checkState(column) == Qt.CheckState.Unchecked:
            # 连同下面子子节点全部设置为取消选中状态
            for f in range(count):
                if item.child(f).checkState != Qt.CheckState.Unchecked:
                    item.child(f).setCheckState(0, Qt.CheckState.Unchecked)
